package com.lxmes.business.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.ZcOrderCostSummaryMapper;
import com.lxmes.business.domain.ZcOrderCostSummary;
import com.lxmes.business.service.IZcOrderCostSummaryService;

/**
 * 订单成本汇总Service业务层处理
 * 
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcOrderCostSummaryServiceImpl implements IZcOrderCostSummaryService 
{
    @Autowired
    private ZcOrderCostSummaryMapper zcOrderCostSummaryMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询订单成本汇总
     * 
     * @param id 订单成本汇总主键
     * @return 订单成本汇总
     */
    @Override
    public ZcOrderCostSummary selectZcOrderCostSummaryById(Long id)
    {
        return zcOrderCostSummaryMapper.selectZcOrderCostSummaryById(id);
    }

    /**
     * 查询订单成本汇总列表
     * 
     * @param zcOrderCostSummary 订单成本汇总
     * @return 订单成本汇总
     */
    @Override
    public List<ZcOrderCostSummary> selectZcOrderCostSummaryList(ZcOrderCostSummary zcOrderCostSummary)
    {
        return zcOrderCostSummaryMapper.selectZcOrderCostSummaryList(zcOrderCostSummary);
    }

    /**
     * 新增订单成本汇总
     * 
     * @param zcOrderCostSummary 订单成本汇总
     * @return 结果
     */
    @Override
    public int insertZcOrderCostSummary(ZcOrderCostSummary zcOrderCostSummary)
    {
        return zcOrderCostSummaryMapper.insertZcOrderCostSummary(zcOrderCostSummary);
    }

    /**
     * 修改订单成本汇总
     * 
     * @param zcOrderCostSummary 订单成本汇总
     * @return 结果
     */
    @Override
    public int updateZcOrderCostSummary(ZcOrderCostSummary zcOrderCostSummary)
    {
        return zcOrderCostSummaryMapper.updateZcOrderCostSummary(zcOrderCostSummary);
    }

    /**
     * 批量删除订单成本汇总
     * 
     * @param ids 需要删除的订单成本汇总主键
     * @return 结果
     */
    @Override
    public int deleteZcOrderCostSummaryByIds(Long[] ids)
    {
        return zcOrderCostSummaryMapper.deleteZcOrderCostSummaryByIds(ids);
    }

    /**
     * 删除订单成本汇总信息
     * 
     * @param id 订单成本汇总主键
     * @return 结果
     */
    @Override
    public int deleteZcOrderCostSummaryById(Long id)
    {
        return zcOrderCostSummaryMapper.deleteZcOrderCostSummaryById(id);
    }
    @Override
    public String imporIteminfo(List<ZcOrderCostSummary> zcOrderCostSummaryList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcOrderCostSummaryList) || zcOrderCostSummaryList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcOrderCostSummary zcOrderCostSummary : zcOrderCostSummaryList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcOrderCostSummary);
                zcOrderCostSummary.setCreateBy(operName);
                zcOrderCostSummary.setUpdateBy(operName);
                this.insertZcOrderCostSummary(zcOrderCostSummary);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
